home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / AIFF_DSP_v15 folder / AIFF_DSP / zerocross.c < prev   
Encoding:
C/C++ Source or Header  |  1994-07-31  |  1.5 KB  |  74 lines  |  [TEXT/KAHL]

  1. /*
  2. FILE:    zerocross.c
  3. PROJECT: Ford grant DSP
  4. AUTHOR:  Ben Denckla
  5. COMMENT: Collects data on the length between zero-crossings.
  6. */
  7.  
  8. #include "aiff.h"
  9. #include <stdio.h>
  10. #include <assert.h>
  11. #include <string.h>
  12.  
  13. #define BINS    40
  14. #define BINSIZE 5
  15.  
  16. int take_input = 1, make_output = 0;
  17.  
  18. static long histogram[BINS+1] = {0};
  19.  
  20. void init_process( void ) {
  21.     if ( (ba.com.wdsi != 8) )
  22.         err( "Cannot process a file with this word size" );
  23.     if ( (ba.com.chan != 1) )
  24.         err( "Cannot process a multichannel file" );
  25. }
  26.  
  27. void term_process ( void ) {
  28.     #define BARMAX 60
  29.     
  30.     int i, barlen;
  31.     long histmax = 0;
  32.     FILE *f;
  33.     char bar[80];
  34.  
  35.     memset( bar, '*', BARMAX );
  36.     
  37.     f = fopen( "zerocross.out", "w" );
  38.  
  39.     for (i=0; i<BINS; i++)
  40.         if (histogram[i] > histmax) histmax = histogram[i];
  41.  
  42.     fprintf( f, "out of range: %ld\n", histogram[BINS] );
  43.     for (i=0; i<BINS; i++) {
  44.         barlen = (float) BARMAX * histogram[i]/histmax;
  45.         fprintf( f, "[%.3d...%.3d): %.4ld %.*s\n",
  46.                 i*BINSIZE, (i+1)*BINSIZE, histogram[i], barlen, bar );
  47.     }    
  48.     fclose( f );
  49. }
  50.  
  51. DEFFUNC( void bin ( long len ) ) {
  52.     assert( len > 0 );
  53.     if ( len < BINS*BINSIZE ) histogram[len/BINSIZE]++;
  54.     else histogram[BINS+1]++;
  55. }
  56.  
  57. void process_samdat ( long buflen ) {
  58.     register char *td, *endd;
  59.     static char last = 0;
  60.     static long abs_pos = 0, last_zero = 0;
  61.     
  62.     td   = d;
  63.     endd = &td[buflen];
  64.     
  65.     do {
  66.         if ( (*td > 0) != (last > 0) ) {
  67.             bin( abs_pos - last_zero );
  68.             last_zero = abs_pos;
  69.         }
  70.         last = *td;
  71.         abs_pos++;
  72.         td++;
  73.     } while ( td < endd );
  74. }